package com.glowdayz.glowmee;

import com.glowdayz.common.ActivityManager;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class PouchFilter extends Activity implements OnClickListener {
	
	private ActivityManager am = ActivityManager.getInstance();
	
	RadioGroup orderRadioGroup = null, genderRadioGroup = null, ageRadioGroup = null, skinRadioGroup = null;
	ImageView okBt = null;
	
	int paramOrder = 0, paramGender = 0, paramAge = 0, paramSkin = 0;
	String paramPouchCategory = null;
	
	RadioButton orderBt1 = null, orderBt2 = null, orderBt3 = null, orderBt4 = null, orderBt5 = null,
			genderBt1 = null, genderBt2 = null, genderBt3 = null,
			ageBt1 = null, ageBt2 = null, ageBt3 = null, ageBt4 = null, ageBt5 = null,
			skinBt1 = null, skinBt2 = null, skinBt3 = null, skinBt4 = null, skinBt5 = null, skinBt6 = null;
	
	boolean noFilter = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pouch_filter);
		
		am.addActivity(this);
		
		ImageView backBt = (ImageView) findViewById(R.id.backBt);
		backBt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
		
		Intent fromIntent = getIntent();
		paramPouchCategory = fromIntent.getStringExtra("PARAM_category");
		paramOrder = fromIntent.getIntExtra("PARAM_order", 0);
		paramGender = fromIntent.getIntExtra("PARAM_gender", 0);
		paramAge = fromIntent.getIntExtra("PARAM_age", 0);
		paramSkin = fromIntent.getIntExtra("PARAM_skin", 0);
		noFilter = fromIntent.getBooleanExtra("PARAM_noFilter", false);
		
		backBt.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (noFilter) {
					Intent intent = new Intent();
					intent.putExtra("PARAM_filter", 1);
					intent.putExtra("PARAM_order", paramOrder);
					intent.putExtra("PARAM_gender", paramGender);
					intent.putExtra("PARAM_age", paramAge);
					intent.putExtra("PARAM_skin", paramSkin);
					intent.putExtra("PARAM_category", paramPouchCategory);
					setResult(Activity.RESULT_OK, intent);
				}
				finish();
			}
		});
		
		TextView textTitle = (TextView) findViewById(R.id.textTitle);
		textTitle.setText("정렬옵션");
		
		orderRadioGroup = (RadioGroup) findViewById(R.id.orderRadioGroup);
		genderRadioGroup = (RadioGroup) findViewById(R.id.genderRadioGroup);
		ageRadioGroup = (RadioGroup) findViewById(R.id.ageRadioGroup);
		skinRadioGroup = (RadioGroup) findViewById(R.id.skinRadioGroup);
		okBt = (ImageView) findViewById(R.id.okBt);
		okBt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
		okBt.setOnClickListener(this);
		
		orderBt1 = (RadioButton) findViewById(R.id.orderBt1);
		orderBt2 = (RadioButton) findViewById(R.id.orderBt2);
		orderBt3 = (RadioButton) findViewById(R.id.orderBt3);
		orderBt4 = (RadioButton) findViewById(R.id.orderBt4);
		orderBt5 = (RadioButton) findViewById(R.id.orderBt5);
		orderBt1.setOnClickListener(this);
		orderBt2.setOnClickListener(this);
		orderBt3.setOnClickListener(this);
		orderBt4.setOnClickListener(this);
		orderBt5.setOnClickListener(this);
		
		genderBt1 = (RadioButton) findViewById(R.id.genderBt1);
		genderBt2 = (RadioButton) findViewById(R.id.genderBt2);
		genderBt3 = (RadioButton) findViewById(R.id.genderBt3);
		genderBt1.setOnClickListener(this);
		genderBt2.setOnClickListener(this);
		genderBt3.setOnClickListener(this);
		
		ageBt1 = (RadioButton) findViewById(R.id.ageBt1);
		ageBt2 = (RadioButton) findViewById(R.id.ageBt2);
		ageBt3 = (RadioButton) findViewById(R.id.ageBt3);
		ageBt4 = (RadioButton) findViewById(R.id.ageBt4);
		ageBt5 = (RadioButton) findViewById(R.id.ageBt5);
		ageBt1.setOnClickListener(this);
		ageBt2.setOnClickListener(this);
		ageBt3.setOnClickListener(this);
		ageBt4.setOnClickListener(this);
		ageBt5.setOnClickListener(this);
		
		skinBt1 = (RadioButton) findViewById(R.id.skinBt1);
		skinBt2 = (RadioButton) findViewById(R.id.skinBt2);
		skinBt3 = (RadioButton) findViewById(R.id.skinBt3);
		skinBt4 = (RadioButton) findViewById(R.id.skinBt4);
		skinBt5 = (RadioButton) findViewById(R.id.skinBt5);
		skinBt6 = (RadioButton) findViewById(R.id.skinBt6);
		skinBt1.setOnClickListener(this);
		skinBt2.setOnClickListener(this);
		skinBt3.setOnClickListener(this);
		skinBt4.setOnClickListener(this);
		skinBt5.setOnClickListener(this);
		skinBt6.setOnClickListener(this);
		
		if (paramOrder > 0) {
			switch (paramOrder) {
			case 1:
				orderRadioGroup.check(R.id.orderBt2);
				orderBt2.setTextColor(Color.parseColor("#ff0000"));
				orderBt1.setTextColor(Color.parseColor("#000000"));
				orderBt3.setTextColor(Color.parseColor("#000000"));
				orderBt4.setTextColor(Color.parseColor("#000000"));
				orderBt5.setTextColor(Color.parseColor("#000000"));
				break;
			case 2:
				orderRadioGroup.check(R.id.orderBt3);
				orderBt3.setTextColor(Color.parseColor("#ff0000"));
				orderBt1.setTextColor(Color.parseColor("#000000"));
				orderBt2.setTextColor(Color.parseColor("#000000"));
				orderBt4.setTextColor(Color.parseColor("#000000"));
				orderBt5.setTextColor(Color.parseColor("#000000"));
				break;
			case 3:
				orderRadioGroup.check(R.id.orderBt4);
				orderBt4.setTextColor(Color.parseColor("#ff0000"));
				orderBt1.setTextColor(Color.parseColor("#000000"));
				orderBt2.setTextColor(Color.parseColor("#000000"));
				orderBt3.setTextColor(Color.parseColor("#000000"));
				orderBt5.setTextColor(Color.parseColor("#000000"));
				break;
			case 4:
				orderRadioGroup.check(R.id.orderBt5);
				orderBt5.setTextColor(Color.parseColor("#ff0000"));
				orderBt1.setTextColor(Color.parseColor("#000000"));
				orderBt2.setTextColor(Color.parseColor("#000000"));
				orderBt3.setTextColor(Color.parseColor("#000000"));
				orderBt4.setTextColor(Color.parseColor("#000000"));
				break;
			}
		}
		
		if (paramGender > 0) {
			switch (paramGender) {
			case 1:
				genderRadioGroup.check(R.id.genderBt2);
				genderBt2.setTextColor(Color.parseColor("#ff0000"));
				genderBt1.setTextColor(Color.parseColor("#000000"));
				genderBt3.setTextColor(Color.parseColor("#000000"));
				break;
			case 2:
				genderRadioGroup.check(R.id.genderBt3);
				genderBt3.setTextColor(Color.parseColor("#ff0000"));
				genderBt1.setTextColor(Color.parseColor("#000000"));
				genderBt2.setTextColor(Color.parseColor("#000000"));
				break;
			}
		}
		
		if (paramAge > 0) {
			switch (paramAge) {
			case 10:
				ageRadioGroup.check(R.id.ageBt2);
				ageBt2.setTextColor(Color.parseColor("#ff0000"));
				ageBt1.setTextColor(Color.parseColor("#000000"));
				ageBt3.setTextColor(Color.parseColor("#000000"));
				ageBt4.setTextColor(Color.parseColor("#000000"));
				ageBt5.setTextColor(Color.parseColor("#000000"));
				break;
			case 20:
				ageRadioGroup.check(R.id.ageBt3);
				ageBt3.setTextColor(Color.parseColor("#ff0000"));
				ageBt1.setTextColor(Color.parseColor("#000000"));
				ageBt2.setTextColor(Color.parseColor("#000000"));
				ageBt4.setTextColor(Color.parseColor("#000000"));
				ageBt5.setTextColor(Color.parseColor("#000000"));
				break;
			case 30:
				ageRadioGroup.check(R.id.ageBt4);
				ageBt4.setTextColor(Color.parseColor("#ff0000"));
				ageBt1.setTextColor(Color.parseColor("#000000"));
				ageBt2.setTextColor(Color.parseColor("#000000"));
				ageBt3.setTextColor(Color.parseColor("#000000"));
				ageBt5.setTextColor(Color.parseColor("#000000"));
				break;
			case 40:
				ageRadioGroup.check(R.id.ageBt5);
				ageBt5.setTextColor(Color.parseColor("#ff0000"));
				ageBt1.setTextColor(Color.parseColor("#000000"));
				ageBt2.setTextColor(Color.parseColor("#000000"));
				ageBt3.setTextColor(Color.parseColor("#000000"));
				ageBt4.setTextColor(Color.parseColor("#000000"));
				break;
			}
		}
		
		if (paramSkin > 0) {
			switch (paramSkin) {
			case 1:
				skinRadioGroup.check(R.id.skinBt2);
				skinBt2.setTextColor(Color.parseColor("#ff0000"));
				skinBt1.setTextColor(Color.parseColor("#000000"));
				skinBt3.setTextColor(Color.parseColor("#000000"));
				skinBt4.setTextColor(Color.parseColor("#000000"));
				skinBt5.setTextColor(Color.parseColor("#000000"));
				skinBt6.setTextColor(Color.parseColor("#000000"));
				break;
			case 2:
				skinRadioGroup.check(R.id.skinBt3);
				skinBt3.setTextColor(Color.parseColor("#ff0000"));
				skinBt1.setTextColor(Color.parseColor("#000000"));
				skinBt2.setTextColor(Color.parseColor("#000000"));
				skinBt4.setTextColor(Color.parseColor("#000000"));
				skinBt5.setTextColor(Color.parseColor("#000000"));
				skinBt6.setTextColor(Color.parseColor("#000000"));
				break;
			case 3:
				skinRadioGroup.check(R.id.skinBt4);
				skinBt4.setTextColor(Color.parseColor("#ff0000"));
				skinBt1.setTextColor(Color.parseColor("#000000"));
				skinBt2.setTextColor(Color.parseColor("#000000"));
				skinBt3.setTextColor(Color.parseColor("#000000"));
				skinBt5.setTextColor(Color.parseColor("#000000"));
				skinBt6.setTextColor(Color.parseColor("#000000"));
				break;
			case 4:
				skinRadioGroup.check(R.id.skinBt5);
				skinBt5.setTextColor(Color.parseColor("#ff0000"));
				skinBt1.setTextColor(Color.parseColor("#000000"));
				skinBt2.setTextColor(Color.parseColor("#000000"));
				skinBt3.setTextColor(Color.parseColor("#000000"));
				skinBt4.setTextColor(Color.parseColor("#000000"));
				skinBt6.setTextColor(Color.parseColor("#000000"));
				break;
			case 5:
				skinRadioGroup.check(R.id.skinBt6);
				skinBt6.setTextColor(Color.parseColor("#ff0000"));
				skinBt1.setTextColor(Color.parseColor("#000000"));
				skinBt2.setTextColor(Color.parseColor("#000000"));
				skinBt3.setTextColor(Color.parseColor("#000000"));
				skinBt4.setTextColor(Color.parseColor("#000000"));
				skinBt5.setTextColor(Color.parseColor("#000000"));
				break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		Index.isLiskPick = false;
		switch (v.getId()) {
		case R.id.okBt:
			
			if (orderRadioGroup.getCheckedRadioButtonId() == R.id.orderBt1) {
				paramOrder = 0;
			} else if (orderRadioGroup.getCheckedRadioButtonId() == R.id.orderBt2) {
				paramOrder = 1;
			} else if (orderRadioGroup.getCheckedRadioButtonId() == R.id.orderBt3) {
				paramOrder = 2;
			} else if (orderRadioGroup.getCheckedRadioButtonId() == R.id.orderBt4) {
				paramOrder = 3;
			} else if (orderRadioGroup.getCheckedRadioButtonId() == R.id.orderBt5) {
				paramOrder = 4;
			}
			
			if (genderRadioGroup.getCheckedRadioButtonId() == R.id.genderBt1) {
				paramGender = 0;
			} else if (genderRadioGroup.getCheckedRadioButtonId() == R.id.genderBt2) {
				paramGender = 1;
			} else if (genderRadioGroup.getCheckedRadioButtonId() == R.id.genderBt3) {
				paramGender = 2;
			}
			
			if (ageRadioGroup.getCheckedRadioButtonId() == R.id.ageBt1) {
				paramAge = 0;
			} else if (ageRadioGroup.getCheckedRadioButtonId() == R.id.ageBt2) {
				paramAge = 10;
			} else if (ageRadioGroup.getCheckedRadioButtonId() == R.id.ageBt3) {
				paramAge = 20;
			} else if (ageRadioGroup.getCheckedRadioButtonId() == R.id.ageBt4) {
				paramAge = 30;
			} else if (ageRadioGroup.getCheckedRadioButtonId() == R.id.ageBt5) {
				paramAge = 40;
			}
			
			if (skinRadioGroup.getCheckedRadioButtonId() == R.id.skinBt1) {
				paramSkin = 0;
			} else if (skinRadioGroup.getCheckedRadioButtonId() == R.id.skinBt2) {
				paramSkin = 1;
			} else if (skinRadioGroup.getCheckedRadioButtonId() == R.id.skinBt3) {
				paramSkin = 2;
			} else if (skinRadioGroup.getCheckedRadioButtonId() == R.id.skinBt4) {
				paramSkin = 3;
			} else if (skinRadioGroup.getCheckedRadioButtonId() == R.id.skinBt5) {
				paramSkin = 4;
			} else if (skinRadioGroup.getCheckedRadioButtonId() == R.id.skinBt6) {
				paramSkin = 5;
			}
			
			Intent intent = new Intent();
			intent.putExtra("PARAM_filter", 1);
			intent.putExtra("PARAM_order", paramOrder);
			intent.putExtra("PARAM_gender", paramGender);
			intent.putExtra("PARAM_age", paramAge);
			intent.putExtra("PARAM_skin", paramSkin);
			intent.putExtra("PARAM_category", paramPouchCategory);
			setResult(Activity.RESULT_OK, intent);
			finish();
			
			break;
			
		case R.id.orderBt1:
			orderBt1.setTextColor(Color.parseColor("#ff0000"));
			orderBt2.setTextColor(Color.parseColor("#000000"));
			orderBt3.setTextColor(Color.parseColor("#000000"));
			orderBt4.setTextColor(Color.parseColor("#000000"));
			orderBt5.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.orderBt2:
			orderBt2.setTextColor(Color.parseColor("#ff0000"));
			orderBt1.setTextColor(Color.parseColor("#000000"));
			orderBt3.setTextColor(Color.parseColor("#000000"));
			orderBt4.setTextColor(Color.parseColor("#000000"));
			orderBt5.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.orderBt3:
			orderBt3.setTextColor(Color.parseColor("#ff0000"));
			orderBt1.setTextColor(Color.parseColor("#000000"));
			orderBt2.setTextColor(Color.parseColor("#000000"));
			orderBt4.setTextColor(Color.parseColor("#000000"));
			orderBt5.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.orderBt4:
			orderBt4.setTextColor(Color.parseColor("#ff0000"));
			orderBt1.setTextColor(Color.parseColor("#000000"));
			orderBt2.setTextColor(Color.parseColor("#000000"));
			orderBt3.setTextColor(Color.parseColor("#000000"));
			orderBt5.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.orderBt5:
			orderBt5.setTextColor(Color.parseColor("#ff0000"));
			orderBt1.setTextColor(Color.parseColor("#000000"));
			orderBt2.setTextColor(Color.parseColor("#000000"));
			orderBt3.setTextColor(Color.parseColor("#000000"));
			orderBt4.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.genderBt1:
			genderBt1.setTextColor(Color.parseColor("#ff0000"));
			genderBt2.setTextColor(Color.parseColor("#222222"));
			genderBt3.setTextColor(Color.parseColor("#222222"));
			break;
			
		case R.id.genderBt2:
			genderBt2.setTextColor(Color.parseColor("#ff0000"));
			genderBt1.setTextColor(Color.parseColor("#222222"));
			genderBt3.setTextColor(Color.parseColor("#222222"));
			break;
			
		case R.id.genderBt3:
			genderBt3.setTextColor(Color.parseColor("#ff0000"));
			genderBt1.setTextColor(Color.parseColor("#222222"));
			genderBt2.setTextColor(Color.parseColor("#222222"));
			break;
			
		case R.id.ageBt1:
			ageBt1.setTextColor(Color.parseColor("#ff0000"));
			ageBt2.setTextColor(Color.parseColor("#000000"));
			ageBt3.setTextColor(Color.parseColor("#000000"));
			ageBt4.setTextColor(Color.parseColor("#000000"));
			ageBt5.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.ageBt2:
			ageBt2.setTextColor(Color.parseColor("#ff0000"));
			ageBt1.setTextColor(Color.parseColor("#000000"));
			ageBt3.setTextColor(Color.parseColor("#000000"));
			ageBt4.setTextColor(Color.parseColor("#000000"));
			ageBt5.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.ageBt3:
			ageBt3.setTextColor(Color.parseColor("#ff0000"));
			ageBt1.setTextColor(Color.parseColor("#000000"));
			ageBt2.setTextColor(Color.parseColor("#000000"));
			ageBt4.setTextColor(Color.parseColor("#000000"));
			ageBt5.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.ageBt4:
			ageBt4.setTextColor(Color.parseColor("#ff0000"));
			ageBt1.setTextColor(Color.parseColor("#000000"));
			ageBt2.setTextColor(Color.parseColor("#000000"));
			ageBt3.setTextColor(Color.parseColor("#000000"));
			ageBt5.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.ageBt5:
			ageBt5.setTextColor(Color.parseColor("#ff0000"));
			ageBt1.setTextColor(Color.parseColor("#000000"));
			ageBt2.setTextColor(Color.parseColor("#000000"));
			ageBt3.setTextColor(Color.parseColor("#000000"));
			ageBt4.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.skinBt1:
			skinBt1.setTextColor(Color.parseColor("#ff0000"));
			skinBt2.setTextColor(Color.parseColor("#000000"));
			skinBt3.setTextColor(Color.parseColor("#000000"));
			skinBt4.setTextColor(Color.parseColor("#000000"));
			skinBt5.setTextColor(Color.parseColor("#000000"));
			skinBt6.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.skinBt2:
			skinBt2.setTextColor(Color.parseColor("#ff0000"));
			skinBt1.setTextColor(Color.parseColor("#000000"));
			skinBt3.setTextColor(Color.parseColor("#000000"));
			skinBt4.setTextColor(Color.parseColor("#000000"));
			skinBt5.setTextColor(Color.parseColor("#000000"));
			skinBt6.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.skinBt3:
			skinBt3.setTextColor(Color.parseColor("#ff0000"));
			skinBt1.setTextColor(Color.parseColor("#000000"));
			skinBt2.setTextColor(Color.parseColor("#000000"));
			skinBt4.setTextColor(Color.parseColor("#000000"));
			skinBt5.setTextColor(Color.parseColor("#000000"));
			skinBt6.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.skinBt4:
			skinBt4.setTextColor(Color.parseColor("#ff0000"));
			skinBt1.setTextColor(Color.parseColor("#000000"));
			skinBt2.setTextColor(Color.parseColor("#000000"));
			skinBt3.setTextColor(Color.parseColor("#000000"));
			skinBt5.setTextColor(Color.parseColor("#000000"));
			skinBt6.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.skinBt5:
			skinBt5.setTextColor(Color.parseColor("#ff0000"));
			skinBt1.setTextColor(Color.parseColor("#000000"));
			skinBt2.setTextColor(Color.parseColor("#000000"));
			skinBt3.setTextColor(Color.parseColor("#000000"));
			skinBt4.setTextColor(Color.parseColor("#000000"));
			skinBt6.setTextColor(Color.parseColor("#000000"));
			break;
			
		case R.id.skinBt6:
			skinBt6.setTextColor(Color.parseColor("#ff0000"));
			skinBt1.setTextColor(Color.parseColor("#000000"));
			skinBt2.setTextColor(Color.parseColor("#000000"));
			skinBt3.setTextColor(Color.parseColor("#000000"));
			skinBt4.setTextColor(Color.parseColor("#000000"));
			skinBt5.setTextColor(Color.parseColor("#000000"));
			break;
		}
		
	}
	
	// 하드웨어 뒤로가기버튼 이벤트 설정.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (noFilter) {
				Intent intent = new Intent();
				intent.putExtra("PARAM_filter", 1);
				intent.putExtra("PARAM_order", paramOrder);
				intent.putExtra("PARAM_gender", paramGender);
				intent.putExtra("PARAM_age", paramAge);
				intent.putExtra("PARAM_skin", paramSkin);
				intent.putExtra("PARAM_category", paramPouchCategory);
				setResult(Activity.RESULT_OK, intent);
			}
			finish();
		}
    	return super.onKeyDown(keyCode, event);
    }
    
    @Override
	protected void onDestroy() {
		super.onDestroy();
		am.removeActivity(this);
	}

}
